package com.turnengine.client.global.game.command;

import org.apache.commons.lang.builder.CompareToBuilder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;

/**
 * The Get Game Host By Name.
 */
public class GetGameHostByName implements IGetGameHostByName {

	/** The command id. */
	public static final short COMMAND_ID = 320;

	/** The name. */
	private String name;
	/** The port. */
	private int port;

	/**
	 * Creates a new Get Game Host By Name.
	 */
	public GetGameHostByName() {
	}

	/**
	 * Creates a new Get Game Host By Name.
	 * @param name the name
	 * @param port the port
	 */
	public GetGameHostByName(String name, int port) {
		setName(name);
		setPort(port);
	}

	/**
	 * Creates a new Get Game Host By Name.
	 * @param iGetGameHostByName the i get game host by name
	 */
	public GetGameHostByName(IGetGameHostByName iGetGameHostByName) {
		setName(iGetGameHostByName.getName());
		setPort(iGetGameHostByName.getPort());
	}

	/**
	 * Returns the name.
	 * @return the name.
	 */
	public String getName() {
		return name;
	}

	/**
	 * Returns the port.
	 * @return the port.
	 */
	public int getPort() {
		return port;
	}

	/**
	 * Sets the name.
	 * @param name the name to set.
	 */
	public void setName(String name) {
		if (name == null) {
			throw new NullPointerException("name");
		}
		this.name = name;
	}

	/**
	 * Sets the port.
	 * @param port the port to set.
	 */
	public void setPort(int port) {
		this.port = port;
	}

	/**
	 * Returns the hash code.
	 * @return the hash code.
	 */
	@Override
	public int hashCode() {
		HashCodeBuilder hash = new HashCodeBuilder(149, 37);
		hash.append(name);
		hash.append(port);
		return hash.toHashCode();
	}

	/**
	 * Returns true if this is equal to the given object.
	 * @param object the object to compare.
	 * @return true if this is equal to the given object.
	 */
	@Override
	public boolean equals(Object object) {
		if (object == this) {
			return true;
		}
		if (object instanceof GetGameHostByName) {
			GetGameHostByName compare = (GetGameHostByName) object;
			EqualsBuilder equals = new EqualsBuilder();
			equals.append(this.name, compare.name);
			equals.append(this.port, compare.port);
			return equals.isEquals();
		}
		return false;
	}

	/**
	 * Compare this to the given object.
	 * @param compare the object to compare to.
	 * @return the result of the comparison.
	 */
	@Override
	public int compareTo(IGetGameHostByName compare) {
		CompareToBuilder builder = new CompareToBuilder();
		builder.append(this.name, compare.getName());
		builder.append(this.port, compare.getPort());
		return builder.toComparison();
	}

	/**
	 * Returns this as a string.
	 * @return this as a string.
	 */
	@Override
	public String toString() {
		ToStringBuilder string = new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE);
		if (name != null) {
			string.append("name", name);
		}
		string.append("port", port);
		return string.toString();
	}

	public short getCommandId() {
		return COMMAND_ID;
	}
}
